/*
 * Renderer.cpp
 *
 *  Created on: Sep 4, 2011
 *      Author: Patrick
 */

#include "MediaCenter.h"
#include "Renderer.h"

extern "C" {
#include "video/Video.h"
}

typedef void (APIENTRY *PFNWGLEXTSWAPCONTROLPROC)(int);
typedef int (*PFNWGLEXTGETSWAPINTERVALPROC)(void);

PFNWGLEXTSWAPCONTROLPROC wglSwapIntervalEXT = NULL;
PFNWGLEXTGETSWAPINTERVALPROC wglGetSwapIntervalEXT = NULL;

Renderer::Renderer() {
	this->sdlSurface = NULL;
	this->state = MCR_STATE_STOPPED;
}

void initializeVSync() {
	char* extensions = (char*)glGetString(GL_EXTENSIONS);
	if (strstr(extensions, "WGL_EXT_swap_control")) {
		wglSwapIntervalEXT = (PFNWGLEXTSWAPCONTROLPROC)wglGetProcAddress("wglSwapIntervalEXT");
		wglGetSwapIntervalEXT = (PFNWGLEXTGETSWAPINTERVALPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
	}
}

int Renderer::start(RendererContext context) {

	this->context = context;

	double previousTicks, currentTicks, delta;

	if (!initialize()) {
		return -1;
	}

	SDL_Event event;
	previousTicks = SDL_GetTicks();

	while (this->state == MCR_STATE_RUNNING) {

		/* Capture events */
		while (SDL_PollEvent(&event)) {
			this->captureEvents(&event);
		}

		/* Handle events */
		this->processEvents();

		/* Calculate ticks sicne last render */
		currentTicks = SDL_GetTicks();
		delta = currentTicks - previousTicks;
		delta = delta == 0 ? 1 : delta;
		previousTicks = currentTicks;

		/* Process view calculations */
		process(delta / 1000.0);

		/* Render the view */
		render();

	}

	this->destroy();
	return 0;

}

int Renderer::stop() {

	this->state = MCR_STATE_STOPPING;
	return 1;

}

int Renderer::initialize() {

	this->state = MCR_STATE_STARTING;

	/* Build the SDL Surface and window */

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		return -1;
	}

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
		return -1;
	}

	SDL_SetVideoMode(this->context.screenWidth, this->context.screenHeight, this->context.screenBPP, this->context.sdlVideoFlags);

	/* Initialize TTF */
	if (TTF_Init()) {
		fprintf(stderr, "TTF initialization failed: %s\n", TTF_GetError());
		return false;
	}
	atexit(TTF_Quit);

	/* Initialize base fonts */
	MCFontCreate(MC_FONT_TTF_DEFAULT_NORMAL, "resources/fonts/DroidSans.ttf", 50);
	MCFontCreate(MC_FONT_TTF_DEFAULT_BOLD, "resources/fonts/DroidSans-Bold.ttf", 50);

	/* Set up GL Context */
	glewInit();
	glClearColor(0, 0, 0, 0);
	glViewport(0, 0, this->context.screenWidth, this->context.screenHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, this->context.screenWidth, this->context.screenHeight, 0, 1, -1);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_TEXTURE_2D);
	glLoadIdentity();
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	/*
	 * PLAY VIDEO
	 */
	MCVideoCreate((char*)"a.mkv", 1280, 720);
	MCVideoPlay();

	/* Enable VSYNC */
	initializeVSync();
	wglSwapIntervalEXT(1);

	this->state = MCR_STATE_RUNNING;

	return 1;
}

int Renderer::destroy() {

	this->state = MCR_STATE_STOPPING;

	MCFontDestroy();
	MCVideoDestroy();
	MCTextureDestroy();
	SDL_Quit();

	this->state = MCR_STATE_STOPPED;

	return 1;

}
